library(RStata)

library(here)

here::i_am("scripts/clean_datasets.R")

# Must point RStata to where stata is installed to run
options("RStata.StataPath"="/Applications/Stata/StataSE.app/Contents/MacOS/stata-se")
options("RStata.StataVersion"= 19L)

# Clean survey 1 (Lucid) --------------------------------------------------

source(here('scripts','clean-survey-1.R'))

temp <- stata(src = here("scripts", "ebalance.do"),
              data.in = survey1[ , c("id", "female", "college",
                                     "hhiBin_49", "hhiBin_99", "hhiBin_149", "hhiBin_150",
                                     "ageBin_18", "ageBin_25", "ageBin_40", "ageBin_60",
                                     "attentive")],
              data.out = TRUE)

survey1 <- merge(survey1, temp[ , c("id", "w_ebal_full", "w_ebal_attentive")],
                 by = "id")

fwrite(survey1, 
       here("data", "survey-1-cleaned.csv"))

# Clean survey 2 (Prolific) -----------------------------------------------

source(here('scripts','clean-survey-2.R'))

temp <- stata(src = here("scripts", "ebalance.do"),
              data.in = survey2[ , c("PROLIFIC_PID","female", "college_bin",
                                     "hhiBin_49", "hhiBin_99", "hhiBin_149", "hhiBin_150",
                                     "ageBin_18", "ageBin_25", "ageBin_40", "ageBin_60",
                                     "attentive")],
              data.out = TRUE)

survey2 <- merge(survey2, temp[ , c("PROLIFIC_PID", "w_ebal_full", "w_ebal_attentive")],
                 by = "PROLIFIC_PID")

fwrite(survey2, 
       here("data", "survey-2-cleaned.csv"))

rm(temp, survey1, survey2)
